package constant.milk.cashbook.tip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.util.MilkTextView;
import constant.milk.cashbook.util.MilkUtil;

public class TipListActivity extends SuperActivity
{
	public static final String TIP_LIST_ID = "TIP_LIST_ID";
			
	private ListView listView;
	
	private HashMap<Integer, String> map;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tip_activity);
		
		setGoneTipButton();
		
		setTitle("도움말");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		listView = (ListView) findViewById(R.id.listView);
	}

	@Override
	protected void setOnClick()
	{
	}

	@Override
	protected void setData()
	{
		map = new TipMap();
		
		Intent intent = getIntent();
		
		int tipList = intent.getIntExtra(TIP_LIST_ID, -1);
		
		ArrayList<String> list = getList(tipList);
				
		listView.setOnItemClickListener(onItemClickListener);
		
		TipListAdapter adapter = new TipListAdapter(
				superContext, R.layout.normal_list_item, list);
		listView.setAdapter(adapter);
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
			Integer id = getMapValue((String) arg0.getItemAtPosition(position));
			
			if (id == null)
			{
				MilkUtil.showToast(superContext, "도움말을 찾을 수 없습니다.");
			}
			
			Intent intent = new Intent(superContext, TipInfoActivity.class);
			intent.putExtra(TipInfoActivity.TIP_ID, id);
			startActivity(intent);
		}
	};
	
	private Integer getMapValue(String value)
	{
		for (Entry<Integer, String> entry : map.entrySet()) 
		{
    		 if(value.equals(entry.getValue()))
    		 {
    			 return entry.getKey();
    		 }
    	}
		
		return null; 
	}
	
	private ArrayList<String> getList(int key)
	{
		ArrayList<String> list = new ArrayList<String>();
		
		int f = 0;
		int l = 1;
		
		if (key == 1)
		{
			f = 1;
			l = 9;
		}
		else if (key == 2)
		{
			f = 9;
			l = 17;
		}
		else if (key == 3)
		{
			f = 17;
			l = 36;
		}
		else if (key == 4)
		{
			f = 36;
			l = 40;
		}
		else if (key == 5)
		{
			f = 40;
			l = 44;
		}
		else if (key == 6)
		{
			f = 44;
			l = 63;
		}
		else if (key == 7)
		{
			f = 63;
			l = 74;
		}
		else if (key == 8)
		{
			f = 74;
			l = 76;
		}
		
		for (int i = f; i < l; i++)
		{
			list.add(map.get(i));
		}
		
		return list;
	}
	
	/**
	 * 현금 리스트 아답터
	 */
	private class TipListAdapter extends ArrayAdapter<String> 
	{
		public TipListAdapter(Context context, int textViewResourceId, ArrayList<String> list) 
		{
			super(context, textViewResourceId, list);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.normal_list_item, null);
			}
			
			String data = getItem(position);
			
			MilkTextView nameTextView = (MilkTextView) v.findViewById(R.id.normalItemNameTextView);
			
			nameTextView.setTextSize(TEXT_SIZE);
			nameTextView.setText(data);
			
			return v;
		}
	}

	@Override
	protected void tipButton()
	{
	}

}
